package com.lgmshare.component.helper;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.DrawableRes;
import androidx.core.app.NotificationCompat;

import com.lgmshare.component.utils.AndroidVersionCheckUtils;

import static androidx.core.app.NotificationCompat.CATEGORY_MESSAGE;

/**
 * 消息通知一般版本适配
 */
public class NotificationHelper {

    private static final int NOTIFICATION_ID = 1089;
    private static final String NOTIFICATION_CHANNEL_ID = "default";
    private static final String NOTIFICATION_CHANNEL_NAME = "应用消息通知";//通知消息管理中心显示名称

    /**
     * 创建一个通知渠道--兼容8.0及以上版本
     *
     * @param context
     */
    public static void createNotificationChannel(Context context) {
        createNotificationChannel(context, NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME);
    }

    /**
     * 创建一个通知渠道--兼容8.0及以上版本
     *
     * @param context
     * @param channelId
     * @param channelName
     */
    public static void createNotificationChannel(Context context, String channelId, String channelName) {
        if (AndroidVersionCheckUtils.hasO()) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            //从铃声管理器
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            //通知渠道
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            // 配置通知渠道的属性
            channel.setDescription("新消息通知");
            // 设置通知出现时的闪灯（如果 android 设备支持的话）
            channel.enableLights(true);
            channel.shouldShowLights();
            //是否在久按桌面图标时显示此渠道的通知
            channel.setShowBadge(true);
            channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
            // 设置通知出现时的震动（如果 android 设备支持的话）
            channel.enableVibration(true);
            channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(channel);
        }
    }

    public static void clearAllNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
    }

    public static void show(Context context, String tag, String title, String content, @DrawableRes int res) {
        show(context, NOTIFICATION_ID, tag, title, content, res);
    }

    public static void show(Context context, int id, String tag, String title, String content, @DrawableRes int res) {
        show(context, null, id, tag, title, content, res);
    }

    /**
     * @param context
     * @param pi      通知处理PendingIntent
     * @param id
     * @param tag
     * @param title
     * @param content
     * @param res
     */
    public static void show(Context context, PendingIntent pi, int id, String tag, String title, String content, @DrawableRes int res) {
        Notification notification = null;
        final Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), res);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //TODO 版本兼容 >8.0
            //创建通知渠道ID
            createNotificationChannel(context);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(res)
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(true)
                    .setCategory(CATEGORY_MESSAGE)
                    .setDefaults(Notification.DEFAULT_ALL);//使用默认的声音、振动、闪光
            if (pi != null) {
                builder.setContentIntent(pi);
            }
            notification = builder.build();
            notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            //TODO 版本兼容 >=22 并且 <26 （5.0-8.0）
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(res)
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(true);
            if (pi != null) {
                builder.setContentIntent(pi);
            }
            notification = builder.build();
            //使用默认的声音
            notification.defaults |= Notification.DEFAULT_SOUND;
            //使用默认的震动
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            //使用默认的声音、振动、闪光
            notification.defaults = Notification.DEFAULT_ALL;
            notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            //TODO 版本兼容 >=16 并且 <22  （4.2-5.0）
            Notification.Builder builder = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(res)
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(false)
                    .setWhen(System.currentTimeMillis());
            if (pi != null) {
                builder.setContentIntent(pi);
            }
            notification = builder.build();
            //使用默认的声音
            notification.defaults |= Notification.DEFAULT_SOUND;
            //使用默认的震动
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            //使用默认的声音、振动、闪光
            notification.defaults = Notification.DEFAULT_ALL;
            notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
        } else {
            //TODO 版本兼容 <16 （4.2)
            notification = new Notification();
            notification.icon = res;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
        }
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(tag, id, notification);
    }
}
